<template>
  <div class="newBuild">
    <div class="mask"></div>
    <div class="new_bd">
      <div class="bd-top">
        <p>新建资料（客户销货）</p>
        <i class="el-icon-close" @click="closeNewClerk"></i>
      </div>
      <div class="bd-info">
        <div class="bd-info-1">
          <p>销货日期</p>
          <el-date-picker v-model="inputDate" value-format="yyyy-MM-dd HH:mm:ss" type="date" :disabled="Boolean(barcodeData.length)"></el-date-picker>
          <p>客户编号</p>
            <!-- <el-input v-model="customerNo" @blur="customerNoBlur" :disabled="supplement"></el-input> -->
            <el-select filterable v-model="customerName"
                      @change="e=>{this.customerNo=e;this.checkDate()}" 
                      :disabled="supplement">
              <el-option v-for="item in customerList" :key="item.value" :label="item.value + '  ' + item.label" :value="item.value"></el-option>
            </el-select>
          <p>单号</p>
          <el-input v-model="inputSequence" disabled></el-input>
          <p>客销序号</p>
          <el-input v-model="sequence" disabled></el-input>
        </div>
        <div class="bd-info-2">
          <p>折数</p>
          <div class="infoBox">
            <el-input v-model="discount" :disabled="!checked1" style="width:80px"></el-input>
            <el-checkbox v-model="checked1" label="改动" border :disabled="supplement || checked3 || checked2"></el-checkbox>
          </div>
          <p>配货方式</p>
          <div class="infoBox">
            <el-select v-model="cooperateWay" :disabled="supplement">
              <el-option v-for="item in cooperateWayList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
            <el-checkbox v-model="checked2" label="返现" border :disabled="supplement"></el-checkbox>
            <p>实收单价</p>
            <div class="infoBox">
              <el-input v-model="onePrice" :disabled="!checked3" style="width:120px"></el-input>
              <el-checkbox v-model="checked3" border :disabled="supplement || checked1"></el-checkbox>
            </div>
          </div>
          <p style="width:70px">备注</p>
          <el-input style="width:160px" v-model="remark" :disabled="supplement"></el-input>
        </div>
        <el-table v-show="!isBarcode" id="codeTable" border :data="codeData" class="tb-edit" style="width: 100%;margin-bottom:15px" max-height="148px" @cell-click="cellClick" :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" :cell-style="{padding:0,height:'36px'}">
          <el-table-column :label="header" width="150"></el-table-column>
          <el-table-column prop="colorName" label="颜色" width="100"></el-table-column>
          <template v-for="i in sizeList">
            <el-table-column :prop="i" :label="i" :key="i+Math.random()">
              <template slot-scope="scope">
                <el-input size="small" class="innerInp" v-model="scope.row[i]" @blur="handleInputBlurResult($event,{index:scope.$index,key:i,result:scope.row[i]})" @keyup.enter.native="inpEnter"></el-input>
                <!-- <span>{{scope.row[i]}}</span> --></template>
            </el-table-column>
          </template>
        </el-table>
        <el-table v-show="isBarcode" border :data="inventoryData" class="tb-edit" style="width:100%;margin-bottom:15px;margin-top:15px" max-height="148px" @cell-click="cellClick" :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" :cell-style="{padding:0,height:'36px'}">
          <el-table-column prop="typeNumber" label="库存"></el-table-column>
          <el-table-column prop="colorName" label="颜色" width="80"></el-table-column>
          <el-table-column prop="c05" label="05-XS" width="70"></el-table-column>
          <el-table-column prop="c07" label="07-S" width="70"></el-table-column>
          <el-table-column prop="c09" label="09-M" width="70"></el-table-column>
          <el-table-column prop="c11" label="11-L" width="70"></el-table-column>
          <el-table-column prop="c13" label="13-XL" width="70"></el-table-column>
          <el-table-column prop="c15" label="15-2L" width="70"></el-table-column>
          <el-table-column prop="c17" label="17-3L" width="70"></el-table-column>
          <el-table-column prop="c19" label="19-4L" width="70"></el-table-column>
          <el-table-column prop="c21" label="21-5L" width="70"></el-table-column>
          <el-table-column prop="c23" label="23-6L" width="70"></el-table-column>
          <el-table-column prop="f" label="F" width="70"></el-table-column>
          <el-table-column prop="total" label="小计" width="70"></el-table-column>
        </el-table>
        <el-table v-show="isBarcode" border :data="customerData" style="width: 100%;margin-top:15px" max-height="148px" :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" :cell-style="{padding:0,height:'36px'}" @cell-click="cellClick">
          <el-table-column prop="no" label="客户编号"></el-table-column>
          <el-table-column prop="kindsNumber" label="货号" width="200"></el-table-column>
          <el-table-column prop="customerRepertory" label="存"></el-table-column>
          <el-table-column prop="customerInput" label="进"></el-table-column>
          <el-table-column prop="customerOut" label="退"></el-table-column>
          <el-table-column prop="customerSell" label="销"></el-table-column>
        </el-table>
        <div class="barcode">
          <p>条码扫描</p>
          <el-switch v-model="isBarcode" active-color="#409EFF" inactive-color="#C0C4CC" :disabled="creating"></el-switch>
        </div>
        <div class="enterBarcode">
          <el-input ref="barcodeInput" @keyup.enter.native="barcodeEnter($event)" v-model="barcode" placeholder="扫描/输入货号或款号，输完按回车"></el-input>
          <span>合计：{{total}}</span>
        </div>
        <el-table height="256px" :data="barcodeData" style="width: 100%" :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" :cell-style="{padding:0,height:'36px'}">
          <el-table-column prop="kindsNumber" label="货号" width="180"></el-table-column>
          <el-table-column prop="colorName" label="颜色"></el-table-column>
          <el-table-column prop="sizeNo" label="尺码编号"></el-table-column>
          <el-table-column prop="sum" label="数量"></el-table-column>
          <el-table-column prop="sellingPrice" label="售价"></el-table-column>
          <el-table-column prop="discount" label="折数"></el-table-column>
          <el-table-column prop="onePrice" label="单价"></el-table-column>
          <el-table-column prop="money" label="金额"></el-table-column>
          <el-table-column prop="commodityName" label="品名"></el-table-column>
        </el-table>
      </div>
      <div class="bd-btn">
        <div>
          <i class="el-icon-question" style="color:#017fff;margin-right:6px;"></i>
          <span>F7 设置扫描数量</span></div>
        <div>
          <el-button @click="closeNewClerk">关闭</el-button>
          <!-- <el-button @click="save" type="primary" plain>保存</el-button> -->
          <el-button @click="continueInput" type="primary">继续录入</el-button></div>
      </div>
    </div>
    <!-- F7弹出框 -->
    <div class="popup" :style="{display:showPopup}">
      <div class="popup_t">
        <p>输入扫描数量</p>
        <i class="el-icon-close" @click="closePopup"></i>
      </div>
      <div class="popup_c">
        <p>请输入扫描数量</p>
        <p>如果不输入则扫描数量为1</p>
        <el-input v-model="scanNum" style="margin-top:10px"></el-input>
        <!-- <input type="text" v-model="scanNum" class="popup_button"> --></div>
      <div class="popup_b">
        <div>
          <el-button @click="closePopup">取消</el-button>
          <el-button type="primary" @click="closePopup">确定</el-button></div>
      </div>
    </div>
  </div>
</template>


<script>
import { getByKindsNumber, sellByKindsNumberContinue, patchByAffirm, getSellInquire, maxSellTime, settleAccountsStatus, getDiscount } from '../../../../network/sellTotal'
import { topInputTime } from '../../../../network/customerInputTotal'
import { getKindsNumberList } from '../../../../network/artNo'
import { getBarCode, searchKindsNumber } from '../../../../network/manufacturer'

import exportMethods from '../../../../components/mixins/exportMethods'
import today from '../../../../components/mixins/today'

export default {
  mixins: [exportMethods,today],
  props: {
    sendData: {
      type: Object,
      default: {}
    },
    flag: {
      type: String,
      default: 'none'
    },
    Str: {
      type:String,
      default: ''
    }
  },
  data(){
    return {
      // 条码规律详情
      manufacturerBarCode: {},
      kindsNumberMap: {},

      originalData: {},

      showPopup:'none',

      inputDate: this.getNowTime(),
      customerNo: '',
      customerName: '',
      customerList: [],
      inputSequence: '',
      sequence: '',
      discount: 10,
      cooperateWay: '',
      cooperateWayList: [
        {value:'配货',label:'配货'},
        {value:'订货',label:'订货'},
        {value:'订断',label:'订断'},
        {value:'追断',label:'追断'}
      ],
      onePrice: '',
      sellingPrice: 0,
      checked1: false,
      checked2: false,
      checked3: false,
      remark: '',

      header: '',
      sizeList: ['7', '9', '1', '3', '5', '7', 'F'],
      codeData: [],
      inventoryData: [],
      customerData: [],

      creating: false,
      isBarcode: false,
      barcode: '',
      barcodeData: [],

      tmpData: {},
      tmpBarcodeData: [],

      scanNum: 1,
      supplement: false,
      jxAgent: document.cookie.match(RegExp('jxAgent'+'=([^;]+)'))[1] == 1 ? true : false,
      maxSellDate: null,
      total: 0,

      isConfirm: false,
      settleAccount: null
    }
  },
  watch: {
    Str(a,b){
      console.log(a)
    },
    sendData(a,b){
      this.isConfirm = Boolean(a.isConfirm)
      this.inputDate = a.inputDate
      this.customerNo = a.customerNo
      this.customerName = a.customerName
      this.customerList = a.customerList
      this.inputSequence = a.inputSequence
      this.sequence = a.sequence
      this.discount = a.discount
      this.cooperateWay = a.cooperateWay
      this.remark = a.remark
    },
    flag(a,b){
      if (a == 'block') {
        this.total = 0
        if (this.creating) this.creating = false
        if (this.$route.query.oid) {
          this.supplement = true
          this.checkDate()
          getSellInquire({
            sellSequence: this.$route.query.oid,
            limit: 99999,
            page: 1
          }).then(res => {
            this.barcodeData = []
            this.kindsNumberMap = {}
            res.data.data.sellInquireInfoList.forEach(e => {
              this.kindsNumberMap[e.kindsNumber] = e.sum
              this.total += Number(e.sum)
              this.barcodeData.unshift(e)
            })
          })
        } else {
          this.supplement = false
          this.sequence = ''
          if (this.customerNo) {
            this.checkDate()
          }
        }
        if (this.isConfirm) {
          settleAccountsStatus({sequence: this.sequence}).then(res => {
            this.settleAccount = res.data.data
          })
        }
      }
    },
    barcode(a,b){
      this.barcode = a.toUpperCase().trim()
    },
    customerNo(a,b){
      this.customerNo = a.toUpperCase().trim()
    },
    inputSequence(a,b){
      if (!a) return
      this.inputSequence = a.toUpperCase().trim()
    },
  },
  created(){
    console.log(this.getNowTime())
  },
  methods: {
    checkDate(){
      // return
      maxSellTime({customerNo: this.customerNo}).then(res => {
        this.maxSellDate = res.data.data ? res.data.data.slice(0,10) : this.getNowTime1()
        // console.log(this.maxSellDate)
        if (this.judgeTime(this.inputDate.slice(0,10),this.maxSellDate)) {
          this.$message.info('时间不合法，不能早于' + this.maxSellDate)
        }
        // console.log(this.maxSellDate)
      })
    },
    sibling(e) {
      let s = []
      let pp = e.parentNode.parentNode.children
      for (let j = 0; j < pp.length; j++) {
        let p = pp[j].children
        for(let i = 0, pl = p.length; i < pl; i++) {
          if(p[i] !== e) s.push(p[i])
        }
      }
      return s
    },
    handleInputBlurResult(e,{index,key,result}) {
      e.target.parentNode.parentNode.parentNode.classList.remove('activeCell')
      let r = /^(0|[1-9][0-9]*|-[1-9][0-9]*)$/
      if (result != 0) {
        result = result.replace(/\b(0+)/gi,"")
      }
      if (!r.test(result) && result) {
        this.$alert('数量请输入整数', '', {
          confirmButtonText: '确定'
        })
        this.codeData[index][key] = 0
        return
      }
      this.codeData[index][key] = result
    },
    cellClick(row, column, cell, event){
      cell.classList.add('activeCell')
      let s = this.sibling(cell)
      s.forEach(e => {
        e.classList.remove('activeCell')
      })
    },
    inpEnter(e){
      e.target.parentNode.parentNode.parentNode.classList.remove('activeCell')
    },

    closeNewClerk(){
      // if (!this.$route.query.oid && this.barcodeData.length) {
      //   this.$alert('数据未保存', '', {
      //     confirmButtonText: '确定'
      //   })
      //   return
      // }
      this.$route.query.oid = ''
      this.save()
      this.codeData = []
      this.customerData = []
      this.inventoryData = []
      this.barcodeData = []
      this.isBarcode = false
      this.barcode = ''
      this.sequence = ''
      this.inputSequence = ''
      this.inputDate = this.getNowTime1()
      this.$emit('closeNew')
    },

    barcodeEnter(){
      if (this.barcode) {
        // getDiscount({kindsNumber:this.barcode,customerNo:this.customerNo}).then(res=>{
        //   console.log(res)
        // })
        if (!this.isBarcode) {
          // 手输款号
          getKindsNumberList({kindsNumberStr: this.barcode}).then(res => {
            // console.log(res)
            if (res.data.data.data.length) {
              this.header = res.data.data.data[0].styleNumber
              this.sellingPrice = res.data.data.data[0].sellingPrice
              this.$emit('inpNumChange')
              // 品牌公司查条码规律详情
              getBarCode({company:res.data.data.data[0].brandedCompany}).then(res => {
                // console.log(res)
                this.manufacturerBarCode = res.data.data[0]
                topInputTime({
                  text: this.header,
                  type: 0
                }).then(res => {
                  this.earliestTime = res.data.data.slice(0,10)
                  // console.log(this.earliestTime)
                  // if (this.judgeTime(this.inputDate.slice(0,10),this.earliestTime.slice(0,10))) {
                    // this.$alert('当前选择的销货时间早于最早进货时间，请重新选择销货时间', '', {
                    //   confirmButtonText: '确定'
                    // })
                    // this.inputDate = this.searchDateWithHour()
                  // }
                })
              })
              // console.log(this.manufacturerBarCode)
              // 输入款号显示中间表和尺码信息   ZZZ-AAS
              res.data.data.data.sort((a,b) => {
                return a.relevanceSize.localeCompare(b.relevanceSize)
              })
              let tmp = res.data.data.data
              this.tmpBarcodeData = tmp
              // console.log(tmp)

              // 中间表  尺码表头
              this.sizeList = [tmp[0].size]
              let isExistSize = 0
              for (const i of tmp) {
                for (const j of this.sizeList) {
                  if (j == i.size) {
                    isExistSize = 0
                    break
                  }
                  isExistSize = 1
                }
                if (isExistSize) {
                  this.sizeList.push(i.size)
                }
              }

              // 中间表
              let tmpCodeData = [{colorNo:tmp[0].colorNo,colorName:tmp[0].colorName}]
              let isExistColor = 0
              for (const i of tmp) {
                for (const j of tmpCodeData) {
                  if (j.colorName == i.colorName) {
                    isExistColor = 0
                    break
                  }
                  isExistColor = 1
                }
                if (isExistColor) {
                  tmpCodeData.push({colorNo:i.colorNo,colorName:i.colorName})
                }
              }
              tmpCodeData.forEach(e => {
                this.sizeList.forEach(f => {
                  e[f] = ''
                })
              })
              this.codeData = tmpCodeData
              let codeTable = document.getElementById('codeTable')
              let inputs = codeTable.getElementsByTagName('input')
              // console.log(inputs)
              let int = setInterval(() => {
                clearInterval(int)
                inputs[0].focus()
                inputs[inputs.length - 1].onkeyup = e => {
                  if (window.event.keyCode == 13) {
                    this.continueInput()
                    inputs[inputs.length - 1].onkeyup = () => {}
                  }
                }
              }, 100)
            } else {
              this.$message('此货号/款号无数据')
            }
          })
        } else {
          getByKindsNumber({
            customerNo: this.customerNo,
            kindsNumber: this.barcode
          }).then(res => {
            if (res.data.code == 200) {
              topInputTime({
                text: this.barcode,
                type: 1
              }).then(res => {
                this.earliestTime = res.data.data.slice(0,10)
                this.continueInput()
              })
              this.customerData = [res.data.data.inputOutSaveRepertoryOne]
              this.inventoryData = [res.data.data.inputOutSaveRepertoryTwo]
              this.inventoryData[0].total = Number(this.inventoryData[0].c05) + Number(this.inventoryData[0].c07) + Number(this.inventoryData[0].c09) + Number(this.inventoryData[0].c11) + Number(this.inventoryData[0].c13) + Number(this.inventoryData[0].c15) + Number(this.inventoryData[0].c17) + Number(this.inventoryData[0].c19) + Number(this.inventoryData[0].c21) + Number(this.inventoryData[0].c23) + Number(this.inventoryData[0].f)
            }
          })
        }
      }
    },

    // 保存
    save(){
      // this.$message({
      //   message: '保存成功',
      //   type: 'success'
      // })
      let send = {
        inputDate : this.inputDate,
        customerNo : this.customerNo,
        customerName : this.customerName,
        inputSequence : this.inputSequence,
        sequence : this.sequence,
        discount : this.discount,
        cooperateWay : this.cooperateWay,
        remark : this.remark,
        supList : this.barcodeData
      }
      // 发送到父组件数据
      this.$emit('saveNew', send)
      this.header = ''
      this.barcodeData = []
      this.originalData = {}
      // this.closeNewClerk()
    },

    // 继续录入
    continueInput(){
      console.log(this.inputDate)
      // return
      if (!this.inputDate) {
        this.$alert('日期不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.customerNo) {
        this.$alert('客户不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.discount) {
        this.$alert('折数不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.earliestTime) {
        this.$alert('无数据无法继续录入', '', {
          confirmButtonText: '确定'
        })
        return
      }
      // 这个郭勇说不用了
      // if (this.judgeTime(this.inputDate.slice(0,10),this.earliestTime.slice(0,10))) {
      //   this.$alert('当前选择的销货时间早于最早进货时间' + this.earliestTime + '，请重新选择销货时间', '', {
      //     confirmButtonText: '确定'
      //   })
      //   return
      // }
      console.log(this.judgeTime(this.inputDate.slice(0,10),this.maxSellDate))
      if (this.judgeTime(this.inputDate.slice(0,10),this.maxSellDate)) {
        this.$alert('时间不合法，不能早于' + this.maxSellDate, '', {
          confirmButtonText: '确定'
        })
        return
      }

      if (this.checked2) {
        if (!this.checked3) {
          this.checked3 = true
          this.$alert('返现需填写实收单价', '', {
            confirmButtonText: '确定'
          })
          return
        }
      }

      if (this.checked3) {
        if (this.onePrice === '') {
          this.$alert('请填写实收单价', '', {
            confirmButtonText: '确定'
          })
          return
        }
        let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
        if (!reg.test(this.onePrice)) {
          this.$alert('实收单价格式不正确', '', {
            confirmButtonText: '确定'
          })
          return
        }
        this.discount = (this.onePrice / this.sellingPrice * 10).toFixed(1)
      }

      // 是否有信息变动
      let isNew = false
      
      if (JSON.stringify(this.originalData) == '{}') {
        // 第一次点读卡，记录原始数据
        this.originalData.inputDate = this.inputDate
        this.originalData.customerNo = this.customerNo
        this.originalData.inputSequence = this.inputSequence
        this.originalData.cooperateWay = this.cooperateWay
        this.originalData.remark = this.remark
      } else {
        // 判断是否修改了数据
        for (const key in this.originalData) {
          if (this.originalData.hasOwnProperty(key)) {
            const e = this.originalData[key]
            if (e != this[key]) {
              // 修改了数据 重新赋值
              isNew = true
              this.originalData.inputDate = this.inputDate
              this.originalData.inputSequence = this.inputSequence
              this.originalData.customerNo = this.customerNo
              this.originalData.cooperateWay = this.cooperateWay
              this.originalData.remark = this.remark
              break
            }
          }
        }
      }

      let validList = []
      if (this.sequence != '' && !isNew) {
        // 有序号并且信息没改，再点读卡，传右表
        // console.log('没有数据变动')
        validList = this.kindsNumberList
      } else {
        // console.log('第一次或有数据变动')
        // 有数据变动，进厂序号改空
        this.sequence = ''
        this.barcodeData = []
      }

      let kindsNumberNumMap = {}
      if (this.isBarcode) {
        // 扫描
        // kindsNumberNumMap[this.barcode] = this.scanNum
      } else {
        // 输入
        let knList = []
        this.codeData.forEach(e => {
          let c = e.colorNo
          this.sizeList.forEach(f => {
            if (this.manufacturerBarCode.styleColorSize) {
              // 型尺色

              if (this.manufacturerBarCode.styleColor) {
                // 款号与尺码之间有 - 

                if (this.manufacturerBarCode.colorSize) {
                  // 色号与尺码之间有 - 
                  knList.push({
                    kindsNumber:`${this.header}-${f}-${c}`,
                    sum:e[f]
                  })
                } else {
                  // 色号与尺码之间没有 -  
                  knList.push({
                    kindsNumber:`${this.header}-${f}${c}`,
                    sum:e[f]
                  })
                }
              } else {
                // 款号与尺码之间没有 - 

                if (this.manufacturerBarCode.colorSize) {
                  // 色号与尺码之间有 -  
                  knList.push({
                    kindsNumber:`${this.header}${f}-${c}`,
                    sum:e[f]
                  }) 
                } else {
                  // 色号与尺码之间没有 - 
                  knList.push({
                    kindsNumber:`${this.header}${f}${c}`,
                    sum:e[f]
                  })
                }
              }
            } else {
              // 型色尺

              if (this.manufacturerBarCode.styleColor) {
                // 款号与颜色之间有 - 

                if (this.manufacturerBarCode.colorSize) {
                  // 色号与尺码之间有 -  
                  knList.push({
                    kindsNumber:`${this.header}-${c}-${f}`,
                    sum:e[f]
                  })
                } else {
                  // 色号与尺码之间没有 -  
                  knList.push({
                    kindsNumber:`${this.header}-${c}${f}`,
                    sum:e[f]
                  })
                }
              } else {
                // 款号与颜色之间没有 - 

                if (this.manufacturerBarCode.colorSize) {
                  // 色号与尺码之间有 -  
                  knList.push({
                    kindsNumber:`${this.header}${c}-${f}`,
                    sum:e[f]
                  })
                } else {
                  // 色号与尺码之间没有 - 
                  knList.push({
                    kindsNumber:`${this.header}${c}${f}`,
                    sum:e[f]
                  }) 
                }
              }
            }
          })
        })
        // console.log(knList)
        for (const i of knList) {
          if (i.sum == '' || i.sum == 0) continue
          kindsNumberNumMap[i.kindsNumber] = i.sum
        }
      }

      if (!this.isBarcode && JSON.stringify(kindsNumberNumMap) == '{}') {
        this.$alert('无数据无法继续录入', '', {
          confirmButtonText: '确定'
        })
        return
      }

      let d = {
        // 是否打开条码扫描（0没打开：输入款号色号 1已打开：货号）
        isScan: this.isBarcode ? 1 : 0,
        // 是否是补单（补货数据（0否 1是））
        isReplenishment: this.$route.query.oid ? 1 :0,
        // 是否返现（0不返现 1返现）
        isReturn: this.checked2 ? 1 : 0,
        // 是否是实收单价（0不不是 1是） 客销用
        isPractical: this.checked3 ? 1 : 0,
        // 返现金额=实收单价
        realMoney: this.checked3 ? this.onePrice : '',
        sellDate: this.$route.query.oid ? this.inputDate : this.inputDate,
        customerNo: this.customerNo,
        inputSequence: this.inputSequence,
        sequence: this.sequence,
        warehouseNo: this.warehouseNo,
        discount: this.discount,
        cooperateWay: this.cooperateWay,
        remark: this.remark,
        // 扫描数量
        scanNum: this.isBarcode ? this.scanNum : '',
        // 货号（条码扫描打开所传值）
        scanKindsNumber: this.isBarcode ? this.barcode : '',
        kindsNumberNumMap,
        flagToken: this.Str
      }
      console.log(d)
      if (this.isConfirm) {
        patchByAffirm(JSON.stringify(d)).then(res => {
          if (res.data.code == 200) {
            if (!this.creating) this.creating = true
            this.barcode = ''
            this.header = ''
            this.codeData = []
            this.$refs.barcodeInput.focus()
            if (res.data.data.validList && res.data.data.validList.length) {
              this.sequence = res.data.data.validList[0].sequence
              let isExist = 0
              res.data.data.validList.forEach(e => {
                this.total += e.sum
                e.isReturn = this.checked2 ? 1 : 0
                for (const i of this.barcodeData) {
                  if (e.kindsNumber == i.kindsNumber) {
                    i.sum += Number(e.sum)
                    i.money = i.sum * i.sellingPrice * i.discount / 10
                    isExist = 1
                    break
                  }
                  isExist = 0
                }
                if (!isExist) {
                  this.barcodeData.unshift(e)
                }
              })
              
              // this.barcodeData.sort((a,b) => {
              //   if (a.styleNumber == b.styleNumber) {
              //     if (a.colorNo == b.colorNo) {
              //       return a.relevanceSize.localeCompare(b.relevanceSize)
              //     } else {
              //       return a.colorNo.localeCompare(b.colorNo)
              //     }
              //   } else {
              //     return a.styleNumber.localeCompare(b.styleNumber)
              //   }
              // })
            }
            if (res.data.data.outOfStock && res.data.data.outOfStock.length) {
              // 库存不足导出
              this.exportRaw(String(res.data.data.outOfStock),'库存不足.txt')
            }
            if (res.data.data.outOfStockPlus && res.data.data.outOfStockPlus.length) {
              // 部分成功导出
              this.exportRaw(String(res.data.data.outOfStockPlus),'部分成功.txt')
            }
            if (this.settleAccount) {
              this.$message.info(`单号:${this.inputSequence}请前往店铺销售查询页面查看`)
            }
          }
        })
      } else {
        sellByKindsNumberContinue(JSON.stringify(d)).then(res => {
          if (res.data.code == 200) {
            if (!this.creating) this.creating = true
            // if (this.isBarcode) {
              this.barcode = ''
              this.header = ''
              this.codeData = []
              this.$refs.barcodeInput.focus()
            // }
            if (res.data.data.validList && res.data.data.validList.length) {
              this.sequence = res.data.data.validList[0].sequence
              let isExist = 0
              res.data.data.validList.forEach(e => {
                this.total += e.sum
                e.isReturn = this.checked2 ? 1 : 0
                for (const i of this.barcodeData) {
                  if (e.kindsNumber == i.kindsNumber) {
                    i.sum += Number(e.sum)
                    i.money = i.sum * i.sellingPrice * i.discount / 10
                    isExist = 1
                    break
                  }
                  isExist = 0
                }
                if (!isExist) {
                  this.barcodeData.unshift(e)
                }
              })
              
              // this.barcodeData.sort((a,b) => {
              //   if (a.styleNumber == b.styleNumber) {
              //     if (a.colorNo == b.colorNo) {
              //       return a.relevanceSize.localeCompare(b.relevanceSize)
              //     } else {
              //       return a.colorNo.localeCompare(b.colorNo)
              //     }
              //   } else {
              //     return a.styleNumber.localeCompare(b.styleNumber)
              //   }
              // })
            }
            if (res.data.data.outOfStock && res.data.data.outOfStock.length) {
              // 库存不足导出
              this.exportRaw(String(res.data.data.outOfStock),'库存不足.txt')
            }
            if (res.data.data.outOfStockPlus && res.data.data.outOfStockPlus.length) {
              // 部分成功导出
              this.exportRaw(String(res.data.data.outOfStockPlus),'部分成功.txt')
            }
          }
        })
      }
      
    },

    // 客户编号失去焦点
    customerNoBlur(){
      if (this.customerNo != '') {
        for (const i of this.customerList) {
          if (this.customerNo == i.value) {
            this.customerName = i.label
            this.checkDate()
            return
          }
        }
        this.$alert('无此客户编号', '', {
          confirmButtonText: '确定'
        })
        this.customerNo = ''
        this.customerName = ''
      } else {
        this.customerName = ''
      }
    },

    // 关闭扫描数量
    closePopup(){
      this.showPopup = 'none'
    },

    // 判断时间先后
    judgeTime(a,b){
      // console.log(a,b)
      // console.log(Number(a.replace(/-/g,'')))
      // console.log(Number(b.replace(/-/g,'')))
      if (Number(a.replace(/-/g,'')) >= Number(b.replace(/-/g,''))) {
        return false
      } else {
        return true
      }
    }
  },
  mounted(){
    let _this = this
    document.onkeydown = function(event){
      let e = event || window.event || arguments.callee.caller.arguments[0]
      if(e && e.keyCode == 118){ // 按 F7
      if(_this.showPopup == 'none'){
        _this.showPopup = 'block'
      }else{
        _this.showPopup = 'none'
      }
      }
    }
  },
}
</script>

<style scoped>
  /* 新建资料 */
  .mask{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
    z-index: 9;
  }
  .new_bd{
    position: fixed;
    top: 7%;
    left: 50%;
    transform: translateX(-50%);
    width: 1075px;
    z-index: 99;
    background-color: white;
    overflow: auto;
  }
  .new_bd .bd-top{
    height: 40px;
    background-color: #f8f8f8;
  }
  .new_bd .bd-top p{
    float: left;
    line-height: 40px;
    padding-left: 20px;
    font-weight: 700;
  }
  .new_bd .bd-top i{
    float: right;
    line-height: 40px;
    padding: 0 16px;
  }

  .new_bd input{
    height: 30px;
    border: solid 1px #DCDFE6;
    border-radius: 5px;
    margin-left: 10px;
  }
  .new_bd input:focus{
    outline: none;
    border: solid 1px #409EFF;
  }
  .new_bd .bd-info-2 .inp-2{
    margin-right: 15px;
  }
  .bd-info{
    padding: 0 15px;
    padding-bottom: 10px;
    /* border-bottom: solid #f2f2f2 2px; */
  }
  .bd-info-1 p,.bd-info-2 p{
    display: inline-block;
    width: 80px;
    text-align: right;
    margin-right: 15px;
  }
  .bd-info-1 p:nth-child(3){
    margin-left: 10px;
  }
  .new_bd .bd-info-1 .inp-2{
    margin-right: 43px;
  }
  .bd-info .bd-info-2{
    margin-bottom: 15px;
  }
  .info_2{
    margin-left: 10px;
  }
  .barcode{
    margin-top: 15px;
  }

  .bd-info .barcode p{
    display: inline;
    margin-right: 15px;
  }
  .bd-info .barcode div{
    margin-top: 15px;
  }

  .barcode-info-1 p, .barcode-info-2 p{
    display: inline-block;
    width: 96px;
    text-align: right;
    margin-right: 9px;
  }
  .barcode-info-1 p:first-child, .barcode-info-2 p:first-child{
    width: 65px;
  }
  .colorBox, .infoBox {
    display: inline;
  }

  .enterBarcode{
    height: 60px;
    background-color: #f2f2f2;
    margin: 15px 0;
  }

  .bd-btn{
    margin: 15px 0 10px 0;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
  }
  .popup{
    width: 400px;
    height: 200px;
    border: 1px solid #dbdbdb;
    box-shadow: 0 2px 2px 2px #dbdbdb;
    z-index: 999;
    position: absolute;
    left: 50%;
    top: 50%;
    margin:-100px 0 0 -200px;
    background: white;
    /* display: block; */
  }
  .popup_t{
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 40px;
    background: rgb(244, 244, 244);
    padding: 8px 15px;
  }
  .popup_t p{
    font-weight: 700;
  }
  .popup_c{
    padding: 8px 15px;
  }
  .popup_button{
    margin: 5px 0 0 0;
    width: 94%;
    height: 30px;
    padding:0 10px;
    border: 1px solid#dbdbdb;
    border-radius: 3px;
  }
  .popup_b{
    margin: 10px 0 0 0;
    padding-left: 230px;
  }
</style>

<style>
  .new_bd .bd-info-1 .el-input, .new_bd .bd-info-2 .el-input{
    /* margin: 10px 20px 0 10px; */
    margin-top: 10px;
    width: 160px;
  }
  .newBuild .el-input__inner{
    height: 30px!important;
  }
  .new_bd .el-input__icon{
    line-height: 30px!important;
  }
  .bd-info .barcode .el-switch{
    margin: 0!important;
  }
  .barcode-info-1 .el-input, .barcode-info-2 .el-input{
    /* margin: 10px 0 0 10px; */
    margin-top: 10px;
    width: 160px;
  }

  .new_bd .enterBarcode .el-input{
    width: 50%;
    margin: 14px 17px;
  }

  .colorBox .el-input{
    width: 73px;
    margin-right: 10px;
  }
  .new_bd .bd-info-1 .infoBox .el-input{
    margin: 0 10px 0 0;
    width: 70px;
  }
  .new_bd .bd-info-2 .infoBox .el-input{
    margin: 0 10px 0 0;
  }
  .new_bd .bd-info-2 .infoBox1 .el-input{
    width: 300px;
  }
  .new_bd .bd-info-2 .infoBox .el-checkbox.is-bordered{
    padding: 3px 8px;
    height: 30px;
    margin-right: 10px;
  }
  .new_bd .bd-info-1 .infoBox .el-input--suffix, .new_bd .bd-info-2 .infoBox .el-input--suffix{
    width: 80px;
  }
  .new_bd .colorBox .el-input--suffix{
    width: 108px;
  }
</style>
